import fs from 'fs'
import path from 'path'
import { ActionFunction, json } from 'remix'

export const action: ActionFunction = async ({ request, params }) => {
    const form = await request.formData()
    const data = form.get('data') as string
    
    if (data) {
        const root = path.join(process.cwd(), 'tasks')
        const strDate = new Date().toJSON()
        const filePath = path.join(root, `${strDate}.json`)
        
        fs.writeFileSync(filePath, data)
        return json({ success: true, storefile: data, filePath })
    }

    return json({ success: false })
}
